/*
 * Regola grammaticale:
 * <term> → <termp> [(MULT <termp>) | (DIV <termp>) | (REM <termp>)]*
 * 
 * Simboli terminali:
 * DIV → "/"
 * REM → "%"
 * MULT → "*"
 */
package composite.number_expression;

import composite.Component;
import composite.NumberExpression;
import composite.number_expression.termp.DivTermp;
import composite.number_expression.termp.MultTermp;
import composite.number_expression.termp.RemTermp;
import java.util.LinkedList;
import java.util.List;
import visitor.Visitor;

/**
 *
 * @author Antonio
 */
public class Term extends NumberExpression {

    private List<Component> termp;

    public Term(Component termp) {

        if (termp == null) {

            throw new NullPointerException();
        }

        this.termp = new LinkedList();
        this.termp.add(termp);
    }

    @Override
    public void add(Component termp) {

        if (termp == null) {
            throw new NullPointerException();
        }

        this.termp.add(termp);
    }

    public List<Component> getStato() {

        return termp;
    }// getStato

    @Override
    public void interpreta(String contesto) {

        for (Component tp : termp) {

            tp.interpreta(contesto);
        }
    }

    @Override
    public void accept(Visitor v) {

        v.visit(this);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();

        for (Component tr : termp) {

            if (tr instanceof DivTermp) {

                sb.append(" / ");
            } else if (tr instanceof MultTermp) {

                sb.append(" * ");
            } else if (tr instanceof RemTermp) {

                sb.append(" % ");
            }

            sb.append(tr);
        }
        return sb.toString();
    }
}
